<?php


namespace App\Http\Controllers\Admin\SystemSet;


use App\Response\Response;
use Illuminate\Http\Request;
use App\Exceptions\BasicException;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\BaseController;

class RechargeConfigController extends BaseController
{
    /**
     * @api {post} /user/recharge-amount/config 添加充值金额配置
     * @apiGroup 系统设置-用户充值金额配置
     * @apiParam {int} client_type 客户端平台类型 1：安卓 2：IOS 3：H5。必须
     * @apiParam {int} sort 排序号，升序排列，必须
     * @apiParam {int} qipao_coin 气泡币，必须
     * @apiParam {numeric} rmb 价格：单位：人民币（元），必须
     * @apiParam {int} status 状态 1；显示 2：隐藏
     * @apiSuccessExample {json} Response-Example
     * {"code":200,"data":[],"message":""}
     */
    public function add(Request $request)
    {
        $param = $request->only('client_type', 'sort', 'qipao_coin', 'rmb', 'status');
        $v = app('validator')->make($param, [
            'client_type' => 'required|integer|in:1,2,3',
            'sort'        => 'required|integer|min:0|max:65535',
            'qipao_coin'  => 'required|integer|min:100',
            'rmb'         => 'required|numeric|min:1',
            'status'      => 'required|integer|in:1,2',
        ]);
        if ($v->fails()) {
            throw new BasicException(10001, $v->errors()->first());
        }

        $res = app('repo_recharge_config')->addOrEditRechargeAmount($param);
        if ($res) {
            return Response::sendData([]);
        }
        throw new BasicException(0, '添加失败');
    }

    /**
     * @api {put} /user/recharge-amount/config 编辑充值金额配置
     * @apiGroup 系统设置-用户充值金额配置
     * @apiParam {int} id 配置id，必须
     * @apiParam {int} sort 排序号，升序排列，必须
     * @apiParam {int} qipao_coin 气泡币，必须
     * @apiParam {numeric} rmb 价格：单位：人民币（元），可以是小数，必须
     * @apiParam {int} status 状态 1；显示 2：隐藏
     * @apiSuccessExample {json} Response-Example
     * {"code":200,"data":[],"message":""}
     */
    public function edit(Request $request)
    {
        $param = $request->only('id', 'sort', 'qipao_coin', 'rmb', 'status');
        $v = app('validator')->make($param, [
            'id'         => 'required|integer|min:1',
            'sort'       => 'required|integer|min:0|max:65535',
            'qipao_coin' => 'required|integer|min:100',
            'rmb'        => 'required|numeric|min:1',
            'status'     => 'required|integer|in:1,2',
        ]);
        if ($v->fails()) {
            throw new BasicException(10001, $v->errors()->first());
        }

        app('repo_recharge_config')->addOrEditRechargeAmount($param);

        return Response::sendData([]);
    }

    /**
     * @api {get} /user/recharge-amount/config 充值金额配置列表
     * @apiGroup 系统设置-用户充值金额配置
     * @apiParam {int} client_type 客户端平台类型 1（默认）：安卓 2：IOS 3：H5。
     * @apiSuccessExample {json} Response-Example
     * {
     * "code": 200,
     * "data": [
     * {
     * "id": 7, 索引id
     * "sort": 4, 排序号
     * "qipao_coin": 3000, 气泡币
     * "rmb": 30, 人民币
     * "status": 1 状态：1：显示；2：隐藏
     * }
     * ],
     * "message": ""
     * }
     */
    public function list(Request $request)
    {
        $param = $request->only('client_type');
        $v = app('validator')->make($param, [
            'client_type' => 'integer|in:1,2,3',
        ]);
        if ($v->fails()) {
            throw new BasicException(10001, $v->errors()->first());
        }

        $clientId = $param['client_type'] ?? 1;

        $list = app('model_recharge_config')
            ->select('id', 'sort', 'qipao_coin', 'rmb', 'status')
            ->where('client_type', $clientId)->get();
        foreach ($list as &$v) {
            $v['rmb'] = round($v['rmb'] / 100, 2);
        }
        $custom_recharge = app('redis')->get('custom_recharge');
        if (empty($custom_recharge)) {
            $custom_recharge = 0;
            app('redis')->set('custom_recharge',0);
        }
        $result['list'] = $list;
        $result['custom_recharge'] = intval($custom_recharge);;

        return Response::sendData($result);
    }

    /**
     * @api {delete} /user/recharge-amount/config 编辑充值金额配置
     * @apiGroup 系统设置-用户充值金额配置
     * @apiParam {int} id 配置id，必须
     * @apiSuccessExample {json} Response-Example
     * {"code":200,"data":[],"message":""}
     */
    public function del(Request $request)
    {
        $param = $request->only('id');
        $v = app('validator')->make($param, [
            'id' => 'required|integer|min:1',
        ]);
        if ($v->fails()) {
            throw new BasicException(10001, $v->errors()->first());
        }


        $res = app('model_recharge_config')->where('id',$param['id'])->delete();
        if ($res) {
            return Response::sendData([]);
        } else {
            throw new BasicException(10001, '删除失败');
        }
    }

    public function setCustomRecharge(Request $request)
    {
        $param = $request->only('custom_recharge');
        $v = app('validator')->make($param, [
            'custom_recharge' => 'required|integer|min:0|max:1',
        ]);
        if ($v->fails()) {
            throw new BasicException(10001, $v->errors()->first());
        }
        app('redis')->set('custom_recharge',$param['custom_recharge']);
        return Response::sendData([]);
    }
}